//
//  DesktopSensor.m
//  Pennyworth
//
//  Created by Chris Karr on 12/24/07.
//  Copyright 2007 Chris J. Karr. All rights reserved.
//

#import "DesktopSensor.h"
#import "Context.h"

#import "SensorsController.h"

#define SENSOR_NAME @"Desktop Activity"

@implementation DesktopSensor

@synthesize lastIdentifier;
@synthesize lastApplication;
@synthesize lastSwitch;

- (void) awakeFromNib
{
	[[SensorsController sharedController] addSensor:SENSOR_NAME];
	[[SensorsController sharedController] addSensor:@"Mounted Volumes"];
	
	self.lastIdentifier = @"";
	self.lastApplication = @"";
	self.lastSwitch = [NSDate date];
	
	[[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update:) userInfo:nil repeats:YES] retain];
}

- (void) update:(NSTimer *) theTimer
{
	if (![[SensorsController sharedController] canFire:SENSOR_NAME])
	{
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
		[note setValue:@"Active Application Duration" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		note = [NSMutableDictionary dictionary];
		[note setValue:@"Active Application" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		note = [NSMutableDictionary dictionary];
		[note setValue:@"Running Applications" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		note = [NSMutableDictionary dictionary];
		[note setValue:@"Mounted Volumes" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		return;
	}		
	
	NSWorkspace * workspace = [NSWorkspace sharedWorkspace];

	NSDictionary * active = [workspace activeApplication];
	
	NSString * app = [active valueForKey:@"NSApplicationBundleIdentifier"];
	
	if (app != nil)
	{
		NSString * appName = [active valueForKey:@"NSApplicationName"];
		NSDate * now = [NSDate date];

		if ([app isEqual:[[NSBundle mainBundle] bundleIdentifier]])
		{
			app = self.lastIdentifier;
			appName = self.lastApplication;
		}
		else
		{
			if (![app isEqualToString:self.lastIdentifier])
				self.lastSwitch = now;

			self.lastIdentifier = app;
			self.lastApplication = appName;
		}

		if (![app isEqual:@""])
		{
			NSString * appValue = [NSString stringWithFormat:@"%@ (%@)", appName, app];

			NSInteger duration = (NSInteger) [now timeIntervalSinceDate:self.lastSwitch];
			
			NSMutableDictionary * note = [NSMutableDictionary dictionary];
			[note setValue:@"Active Application Duration" forKey:OBSERVATION_SENSOR];
			[note setValue:[NSNumber numberWithInteger:duration] forKey:OBSERVATION_OBSERVATION];
			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
			
			note = [NSMutableDictionary dictionary];
			
			[note setValue:@"Active Application" forKey:OBSERVATION_SENSOR];
			[note setValue:appValue forKey:OBSERVATION_OBSERVATION];

			[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];

		}
	}
	else
		app = @"";	

	NSArray * apps = [workspace launchedApplications];
	NSMutableArray * otherApps = [NSMutableArray array];

	for (NSDictionary * d  in apps)
	{
		NSString * identifier = [d valueForKey:@"NSApplicationBundleIdentifier"];
		NSString * name = [d valueForKey:@"NSApplicationName"];
		
		// if (![app isEqual:identifier] && ![identifier isEqual:[[NSBundle mainBundle] bundleIdentifier]])
		if (![identifier isEqual:[[NSBundle mainBundle] bundleIdentifier]])
			[otherApps addObject:[NSString stringWithFormat:@"%@ (%@)", name, identifier]];
	}

	if ([otherApps count] > 0)
	{
		[otherApps sortUsingSelector:@selector(compare:)];
		
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
		[note setValue:@"Running Applications" forKey:OBSERVATION_SENSOR];
		[note setValue:otherApps forKey:OBSERVATION_OBSERVATION];

		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
	}

	if (![[SensorsController sharedController] canFire:@"Mounted Volumes"])
	{
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
		[note setValue:@"Mounted Volumes" forKey:OBSERVATION_SENSOR];
		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
		
		return;
	}		
	
	NSMutableArray * mounts = [NSMutableArray arrayWithArray:[workspace mountedLocalVolumePaths]];

	if ([mounts count] > 0)
	{
		[mounts sortUsingSelector:@selector(compare:)];
		
		NSMutableDictionary * note = [NSMutableDictionary dictionary];
	
		[note setValue:@"Mounted Volumes" forKey:OBSERVATION_SENSOR];
		[note setValue:mounts forKey:OBSERVATION_OBSERVATION];

		[[NSNotificationCenter defaultCenter] postNotificationName:OBSERVATION_UPDATE object:self userInfo:note];
	}
}

@end
